// pages/subscribe/subscribe.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    images: [{
      id: 1,
      src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2977125337,1161551203&fm=26&gp=0.jpg',
      name: '照片01',
      data: '2017/11/1'
    }, {
      id: 2,
      src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2977125337,1161551203&fm=26&gp=0.jpg',
      name: '照片02',
      data: '2017/11/2'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLabList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getLabList() {
    var that = this
    wx.request({
      url: app.globalData.API + '/equipment/list',
      data: {},
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log("[subscribe.js] /equipment/list 请求成功", res)
        if (res.data.code == 200) {
          that.setData({
            labList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /equipment/list 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  subscribe(e) {
    console.log(e.target.dataset.id)
    var id = e.target.dataset.id
    var that = this
    wx.request({
      url: app.globalData.API + '/equipment/borrow',
      data: {
        equipment_id: id
      },
      method: "post",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log("[borrow.js] /equipment/borrow 请求成功", res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.message,
          })
        }
        else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[borrow.js] /lab/subscribe 请求失败", res.data)
      },
      complete: res => {

      }
    })
  }
})